package com.ljx.recruitment.user.service.impl.email;

import cn.hutool.core.util.RandomUtil;
import com.ljx.recruitment.common.constant.CacheConstants;
import com.ljx.recruitment.common.exception.ApiException;
import com.ljx.recruitment.common.util.RedisUtil;
import com.ljx.recruitment.user.domain.vo.req.EmailSendReq;
import com.ljx.recruitment.user.domain.vo.resp.EmailSendResp;
import com.ljx.recruitment.user.service.EmailSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.ljx.recruitment.common.constant.CacheConstants.REGISTER_EMAIL_EXPIRE_TIME;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-30  22:19
 */
@Slf4j
public class RegisterEmailSendServiceImpl implements EmailSendService {

    @Override
    public EmailSendResp sendEmail(JavaMailSender mailSender, EmailSendReq req) {
        String requestId = RandomUtil.randomString(10);
        setRegisterCode(req, requestId);
        try {
           MimeMessage message = mailSender.createMimeMessage();
           MimeMessageHelper helper = new MimeMessageHelper(message, true);
           helper.setFrom(req.getFrom());
           helper.setTo(req.getTo());
           helper.setSubject(getTitle(req.getDetailsMap()));
           helper.setText(getContent(req.getDetailsMap()), true);
           mailSender.send(message);
       } catch (Exception e){
           log.error("发送注册邮件出错，{}", e.getMessage());
           throw new ApiException("发送注册邮件出错");
       }

        return EmailSendResp.builder()
                .email(req.getTo())
                .requestId(requestId)
                .build();
    }

    private void setRegisterCode(EmailSendReq req, String requestId) {
        String code = RandomUtil.randomNumbers(6);
        Map<String, String> map = req.getDetailsMap();
        map.put("code", code);
        RedisUtil.set(CacheConstants.getKey(CacheConstants.REGISTER_EMAIL, requestId), code,
                REGISTER_EMAIL_EXPIRE_TIME, TimeUnit.MINUTES);
        req.setDetailsMap(map);
    }

    @Override
    public String getTitle(Map<String, String> params) {
        return "注册账号邮件代发";
    }

    @Override
    public String getContent(Map<String, String> params) {
        return String.format(
                "<html>" +
                        "<body style='font-family: Arial, sans-serif; line-height: 1.5;'>" +
                        "<div style='max-width: 600px; margin: auto; padding: 20px; border: 1px solid #e0e0e0; border-radius: 5px;'>" +
                        "<h2 style='color: #333;'>尊敬的用户，</h2>" +
                        "<p>您好！感谢您使用我们的服务。</p>" +
                        "<p>您的验证码是：<span style='font-size: 18px; font-weight:600'>%s</span>，有效期为 <strong>5分钟</strong>。</p>" +
                        "<p>请您在有效期内使用此验证码进行操作。</p>" +
                        "<p>如您并未发起此请求，您可以安全地忽略此邮件。</p>" +
                        "<p>为了保障您的账户安全，请勿将此验证码透露给他人。</p>" +
                        "<p>感谢您的配合与支持！</p>" +
                        "<p>祝您生活愉快！</p>" +
                        "<footer style='margin-top: 20px; font-size: 12px; color: #777;'>本邮件由系统自动生成，请勿回复。</footer>" +
                        "</div>" +
                        "</body>" +
                        "</html>",
                params.get("code")
        );
    }


}
